//引入api
import { getAdminBrand, postPutimg ,postAddbrand} from '@/request/api.js';
//引入element
import { Message } from 'element-ui';
//引入路由
import router from '@/router'

export default {
    namespaced: true,
    state: {
        count: 100,
        currentPage: 1,
        pageSize: 10,
        brandList: [{}],
        //用于提交的数据
        subsign: {
            name: '',
            pic_url: '',
            list_pic_url: '',
            app_list_pic_url: '',
            floor_price: '',
            is_new: false,
            is_show: false,
            sort_order: '',
            simple_desc: ''
        }
    },
    mutations: {
        getBrandList(state, payload) {
            state.count = payload.count;
            state.brandList = payload.data;
            state.pageSize = payload.pageSize;
            state.currentPage = payload.currentPage;
        },
        //修改本地vuex
        goodsGetvalue(state, payload) {
            state.subsign = payload
        },
        //获取编辑数据
        goodseditsubsign(state, payload) {
            console.log('编辑前的参数', state.subsign)

            state.subsign = { ...payload }
            //对值做相应调整
            if (payload.is_show) {
                state.subsign.is_show = true
            } else {
                state.subsign.is_show = false
            }
            if (payload.is_new) {
                state.subsign.is_new = true
            } else {
                state.subsign.is_new = false
            }
        },
    },
    actions: {
        //发起请求
        goodsAddgoods({ commit, state }) {
            let is_show = 0;
            let is_new = 0;
            if (state.subsign.is_show) {
                is_show = 1
            } else {
                is_show = 0
            }   
            if (state.subsign.is_new) {
                is_new = 1
            } else {
                is_new = 0
            }          
            postAddbrand({
                ...state.subsign,
                is_new,
                is_show
            }).then(res => {

                if (res) {
                    if(state.subsign.id){
                        Message({
                            message:'修改成功',
                            type:'success'
                        })
                    }else{
                        Message({
                            message:'添加成功',
                            type:'success'
                        })
                    }
                    router.push('/brand')
                } else {
                    console.log('请求失败')
                }
            })
        },
        //content页面数据请求
        brandActions({ commit, state }, payload = payload || {}) {
            // console.log(state.currentPage)
            getAdminBrand({
                page: payload.currnet || state.currentPage,
                size: payload.size || state.pageSize
            }).then(res => {
                if (res.errno == 0) {
                    // console.log(res.data)
                    commit('getBrandList', res.data)
                    // console.log(state.brandList)
                }
            })
        },
        //上传图片
        categoryPutimg01({ commit, state }, payload) {
            console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                console.log(res.data)
                state.subsign.pic_url = res.data.fileUrl
            })
        },
        categoryPutimg02({ commit, state }, payload) {
            console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                console.log(res.data)
                state.subsign.list_pic_url = res.data.fileUrl
            })
        },
        categoryPutimg03({ commit, state }, payload) {
            console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                console.log(res.data)
                state.subsign.app_list_pic_url = res.data.fileUrl
            })
        },
    }
}